home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue31 / delcom / ClassFactory.pas next >
Encoding:
Pascal/Delphi Source File  |  1998-02-04  |  7.1 KB  |  188 lines

  1. unit ClassFactory;
  2. {********************************************************************}
  3. {**  Unit : ClassFactory                                           **}
  4. {**                                                                **}
  5. {**  Description : This unit contains the class factory for the    **}
  6. {**  BMPProp shell extension class.                                **}
  7. {**                                                                **}
  8. {**  Version History :                                             **}
  9. {**                                                                **}
  10. {**  A0.01     Initial version started                   27/10/96  **}
  11. {**                                                                **}
  12. {** Copyright ⌐1996 David J. Fiddes                                **}
  13. {********************************************************************}
  14. interface
  15. uses
  16.   Windows, { Standard Unit }
  17.   OLE2,
  18.  
  19.   Global; { Application specific units }
  20.  
  21. type
  22. {********************************************************************}
  23. {**  Object : TClassFactory                                        **}
  24. {**                                                                **}
  25. {**  Description : This is the Class Factory for the shell         **}
  26. {**  extension. It creates a copy of TProperties, the main         **}
  27. {**  object of the shell extension.                                **}
  28. {**                                                                **}
  29. {**  Version History :                                             **}
  30. {**                                                                **}
  31. {**  A0.01     Initial version started                   27/10/96  **}
  32. {**                                                                **}
  33. {********************************************************************}
  34.   TClassFactory = class(IClassFactory)
  35.  
  36.     FRefCount : longint; { reference count for the object }
  37.  
  38.     constructor Create;
  39.     destructor Destroy; override;
  40.  
  41.     {** IUnknown interface **}
  42.     function QueryInterface(const iid: TIID; var obj):HResult; override;
  43.     function AddRef:Longint; override;
  44.     function Release:Longint; override;
  45.  
  46.     {** IClassFactory **}
  47.     function CreateInstance(unkOuter: IUnknown; const iid: TIID; var obj):HResult; override;
  48.     function LockServer(fLock: BOOL): HResult; override;
  49.   end;
  50.  
  51.  
  52. {********************************************************************}
  53. implementation
  54. uses
  55.   Properties; { Application Specific Unit }
  56.  
  57.  
  58. {********************************************************************}
  59. {**  Method : TClassFactory.Create                                 **}
  60. {**                                                                **}
  61. {**  This is the constructor for the class factory. The reference  **}
  62. {**  counter for the DLL is incremented and the object counter     **}
  63. {**  setup                                                         **}
  64. {********************************************************************}
  65. constructor TClassFactory.Create;
  66. begin
  67.   inc(RefThisDLL); { I exist .'. keep me alive! }
  68.   FRefCount:=0; {zero to start - incremented by QueryInterface}
  69. end;
  70.  
  71.  
  72. {********************************************************************}
  73. {**  Method : TClassFactory.Destroy                                **}
  74. {**                                                                **}
  75. {**  This is the destructor for the class factory. The reference   **}
  76. {**  counter for the DLL is decremented so that it cleans up.      **}
  77. {********************************************************************}
  78. destructor TClassFactory.Destroy;
  79. begin
  80.   dec(RefThisDLL);
  81. end;
  82.  
  83.  
  84. {********************************************************************}
  85. {**  Method : TClassFactory.QueryInterface                         **}
  86. {**                                                                **}
  87. {**  This is the method return a pointer to the requested          **}
  88. {**  interface. It will return a pointer for the IUnknown and      **}
  89. {**  IClassFacotry interfaces.                                     **}
  90. {********************************************************************}
  91. function TClassFactory.QueryInterface(const iid: TIID; var obj):HResult;
  92. var
  93.   hr : hResult;
  94. begin
  95.   pointer(obj):=nil;
  96.   hr:=E_NOINTERFACE;
  97.  
  98.   if ISEqualIID(iid,IID_IUnknown) OR ISEqualIID(iid,IID_IClassFactory) then
  99.   begin
  100.     pointer(obj):=Self;
  101.     hr:=NOERROR;
  102.     AddRef; {if it's OK then add a reference to it}
  103.   end;
  104.  
  105.   Result:=hr;
  106. end;
  107.  
  108.  
  109. {********************************************************************}
  110. {**  Method : TClassFactory.AddRef                                 **}
  111. {**                                                                **}
  112. {**  This method increments the object reference counter.          **}
  113. {********************************************************************}
  114. function TClassFactory.AddRef:longint;
  115. begin
  116.   inc(FRefCount);
  117.   AddRef:=FRefCount;
  118. end;
  119.  
  120.  
  121. {********************************************************************}
  122. {**  Method : TClassFactory.Release                                **}
  123. {**                                                                **}
  124. {**  This method decrements the object reference counter and       **}
  125. {**  unloads the object if the counter is zero.                    **}
  126. {********************************************************************}
  127. function TClassFactory.Release:longint;
  128. begin
  129.   dec(FRefCount);
  130.   Result:=FRefCount;
  131.   if FRefCount=0 then Free;
  132. end;
  133.  
  134.  
  135. {********************************************************************}
  136. {**  Method : TClassFactory.CreateInstance                         **}
  137. {**                                                                **}
  138. {**  This method creates and instance of the TPropertiesExt        **}
  139. {**  object and checks that it's OK.                               **}
  140. {********************************************************************}
  141. function TClassFactory.CreateInstance( unkOuter: IUnknown; const iid: TIID; var obj):HResult;
  142. var
  143.   hr    : hResult;
  144.   MyObj : TPropertiesExt;
  145. begin
  146.   pointer(obj):=nil;
  147.   hr:=E_OUTOFMEMORY;
  148.   if unkOuter<>nil then
  149.   begin
  150.     Result:=CLASS_E_NOAGGREGATION;
  151.     exit;
  152.   end;
  153.  
  154.   MyObj:=TPropertiesExt.Create; {create object and check for thicky errors}
  155.   if MyObj=nil then
  156.   begin
  157.     Result:=hr;
  158.     exit;
  159.   end;
  160.  
  161.   hr:=MyObj.QueryInterface(iid,obj); {see if it supports the interface}
  162.  
  163.   if Failed(hr) then {if an error then get rid of it}
  164.     MyObj.Destroy;
  165.  
  166.   Result:=hr;
  167. end;
  168.  
  169.  
  170. {********************************************************************}
  171. {**  Method : TClassFactory.LockServer                             **}
  172. {**                                                                **}
  173. {**  This method increments the DLL reference counter when applying**}
  174. {**  a lock and decrements to unlock the COM server.               **}
  175. {********************************************************************}
  176. function TClassFactory.LockServer(fLock: BOOL): HResult;
  177. begin
  178.   if fLock then
  179.     inc(RefThisDLL)
  180.   else
  181.     dec(RefThisDLL);
  182.   Result:=S_OK;
  183. end;
  184.  
  185.  
  186. end.
  187.  
  188.